﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExcelFileQueryTool
{
    public partial class frmClientList : Form
    {
        private CrmPeopleService _peopleService = new CrmPeopleService(AppConfig.DBConnectionString);
        private static string[] HIDE_COLUMNS = { "ClientId", "ClientName", "ProductId", "TotalMoney", "Status", "CreatedTime", "LastUpdatedTime", "Note", "IsPaidOff", "BillType" };
        public frmClientList()
        {
            InitializeComponent();

            //var types = _peopleService.GetAllTypeCodes();
            //if (types != null && types.Count() > 0)
            //{
            //    foreach (var t in types)
            //        cboxType.Items.Add(t);

            //    cboxType.SelectedIndex = 0;
            //}

            cboxType.Items.AddRange(AppConfig.ClientTypes);
            cboxType.SelectedIndex = 0;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            string name = txtName.Text.Trim();
            string phone = txtPhone.Text.Trim();
            string type = cboxType.SelectedItem.ToString();
            if (type == "所有")
                type = null;

            dgvData.DataSource = _peopleService.QueryList(name, type, phone);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var id = (int)dgvData["Id", e.RowIndex].Value;

            using (var f = new frmClientDetail(id))
            {
                if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    LoadData();
            }
        }

        private void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == (int)Keys.Enter)
                LoadData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvData.SelectedRows == null || dgvData.SelectedRows.Count == 0)
                return;

            if (MessageBox.Show("确定要删除当前选定的客户吗？", "提示", MessageBoxButtons.YesNo) != System.Windows.Forms.DialogResult.Yes)
                return;

            var id = Convert.ToInt32(dgvData.SelectedRows[0].Cells["Id"].Value);
            _peopleService.Delete(id);

            if (MessageBox.Show("删除数据库中的客户数据成功，需要删除对应的客户文件吗？", "提示", MessageBoxButtons.YesNo) != System.Windows.Forms.DialogResult.Yes)
                return;

            var type = Convert.ToString(dgvData.SelectedRows[0].Cells["TypeCode"].Value);
            var name = Convert.ToString(dgvData.SelectedRows[0].Cells["Name"].Value);
            var file = Path.Combine(AppConfig.SourceFolder, type, "[对数表]" + AppConfig.GetSourceName(name) + ".xlsx");
            if (File.Exists(file))
            {
                try
                {
                    File.Delete(file);
                }
                catch (Exception ex)
                {
                    MessageDispatcherManager.DispatchMessage(
                            new AppMessage
                            {
                                Time = DateTime.Now,
                                Level = AppMessageLevel.Warn,
                                Title = "删除客户文件失败",
                                Content = ex.Message
                            });
                }
            }
            else
            {
                MessageDispatcherManager.DispatchMessage(
                            new AppMessage
                            {
                                Time = DateTime.Now,
                                Level = AppMessageLevel.Warn,
                                Title = "客户文件不存在",
                                Content = "找不到客户文件[" + file + "]，请手动查找进行删除！"
                            });
            }

            LoadData();
        }
    }
}
